<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past

$cache_time=2;
$OJ_CACHE_SHARE=false;
require_once ("../config/path_config.php");
require_once(ROOTDIR."admin/admin-header.php");
include_once (ROOTDIR."common/db_utils.php");
require_once(ROOTDIR."include/const.inc.php");
require_once(ROOTDIR."include/my_func.inc.php");
require_once(ROOTDIR."common/result_id.php");
require_once(ROOTDIR . "config/privliege_config.php");
require_once(ROOTDIR."include/set_get_key.php");

$view_title = "解题状态列表";

/////////////////////////////////////////////////////
/// 获取请求参数
/////////////////////////////////////////////////////

// 题目ID
$problem_id=-1;
if ( isset($_GET['problem_id']) && $_GET['problem_id']!="" ) {
    $problem_id=intval($_GET['problem_id']);
}

// 用户ID
$user_id="";
if ( isset($_GET['user_id']) ) {
    $user_id=trim($_GET['user_id']);
}

// 语言类别
$language=-1;
if (isset($_GET['language'])) {
    $language=intval($_GET['language']);
    if ( $language > count($language_ext) || $language<0 ) {
        $language=-1;
    }
}

// 解决题状态,正确、错误、超时
$result = -1;
if ( isset($_GET['jresult']) ) {
    $result = intval($_GET['jresult']);
    if ( $result > 12 || $result < 0 ) {
        $result=-1;
    }
}

// 生成where子句
$sqlWhere = "";
$args = array();

if ( $problem_id > 0 ) {
    $sqlWhere = DbUtil::genWhereSql($sqlWhere, 'problem_id');
    array_push($args, $problem_id);
}

if ( is_valid_user_name($user_id) && $user_id!="" ) {
    $sqlWhere = DbUtil::genWhereSql($sqlWhere, 'user_id');
    array_push($args, $user_id);
}

if ( $language!=-1 ){
    $sqlWhere = DbUtil::genWhereSql($sqlWhere, 'language');
    array_push($args, $language);
}

if ( $result!= -1 ){
    $sqlWhere = DbUtil::genWhereSql($sqlWhere, 'result');
    array_push($args, $result);
}

$sql = "select * from `solution`  ".$sqlWhere." ORDER BY `solution_id` DESC limit 20";

$result = DbUtil::getInstance()->query2($sql, $args);
$rows_cnt=0;
if( isset($result) ) {
    $rows_cnt = count($result);
}

// 生成模板展示信息
$view_status = Array();

for ($i=0; $i < $rows_cnt; $i++)
{
    $row=$result[$i];
    // 解决方案ID
    $view_status[$i][0]=$row['solution_id'];
    // 用户ID
    $view_status[$i][1]= "<a href='../userinfo.php?user=".$row['user_id']."'>".$row['user_id']."</a>";
    // 姓名
    $view_status[$i][2]=$row['user_name'];

    // 题目ID
    $view_status[$i][3]="<div class=center>";
    $view_status[$i][3] .= "<a href='../problem.php?id=".$row['problem_id']."'>".$row['problem_id']."</a></div>";


    // 评测结果提示语和标签文字
    $resultID = intval($row['result']);
    $resultMsg = '';
    $resultTips = '';
    switch( $resultID ) {
        case ResultID::ID_AC:
            $resultTips = $MSG_HELP_AC;
            $resultMsg = '正确';
            break;
        case ResultID::ID_PE:
            $resultMsg = '格式错误';
            $resultTips = $MSG_HELP_PE;
            break;
        case ResultID::ID_WA:
            $resultMsg = '答案错误';
            $resultTips = $MSG_HELP_WA;
            break;
        case ResultID::ID_TLE:
            $resultTips = $MSG_HELP_TLE;
            $resultMsg = '时间超限';
            break;
        case ResultID::ID_MLE:
            $resultTips = $MSG_HELP_MLE;
            $resultMsg = '内存超限';
            break;
        case ResultID::ID_OLE:
            $resultTips = $MSG_HELP_OLE;
            $resultMsg = '输出超限';
            break;
        case ResultID::ID_RE:
            $resultTips = $MSG_HELP_RE;
            $resultMsg = '运行错误';
            break;
        case ResultID::ID_CE:
            $resultTips = $MSG_HELP_CE;
            $resultMsg = '编译错误';
            break;
        default:
            $resultMsg = '正在评测';
            break;
    }

    // 评测状态样式
    $judge_color=Array(
        "btn gray",
        "btn btn-info",
        "btn btn-warning",
        "btn btn-warning",
        "btn btn-success",
        "btn btn-danger",
        "btn btn-danger",
        "btn btn-warning",
        "btn btn-warning",
        "btn btn-warning",
        "btn btn-warning",
        "btn btn-warning",
        "btn btn-warning",
        "btn btn-info");
    $resultClass = $judge_color[$row['result']];

    // 展示正确率
    if ( isset($row['pass_rate']) )
    {
        // 需考虑浮点误差
        if ( $row['pass_rate'] > 0 && $row['pass_rate'] < 0.98 ) {
            $resultMsg .= (100-$row['pass_rate']*100)."%"; // 错误率
        }
        else if ( $row['pass_rate'] >= 0.98 ) {
            $resultMsg .= "100%";
        }
    }

    // 编译错误
    if ( $resultID == ResultID::ID_CE  ) {
        $resultDetailUrl =  "../ceinfo.php?sid=".$row['solution_id'];
    }

    // 获取评测详情的链接
    $resultDetailUrl = '';
    // 答案部分通过
    if ( $resultID != ResultID::ID_AC )
    {
        $resultDetailUrl = "../reinfo.php?sid=".$row['solution_id'];
    }

    $view_status[$i][4] ="<span class='hidden' style='display:none' result='".$row['result']."' ></span>";
    // 答案正确，不添加跳转链接
    if ( strlen($resultDetailUrl) == 0 ) {
        $view_status[$i][4] .= "<a class='".$resultClass."'".">".$resultMsg."</a>";
    }
    else {
        $view_status[$i][4] .= "<a href='".$resultDetailUrl."'"." class='".$resultClass."'".">".$resultMsg."</a>";
    }

    // 展示内存占用和运行时间
    if ( $resultID >= ResultID::ID_AC ){
        // 评测完成
        $view_status[$i][5]= "<div id=center>".$row['memory']."</div>";
        $view_status[$i][6]= "<div id=center>".$row['time']."</div>";

        $view_status[$i][7] = "<a target=_blank href='../showsource.php?id=".$row['solution_id']."'>".$language_name[$row['language']]."</a>";
        $view_status[$i][8]= $row['code_length']." B";
    }
    else {
        // 评测进行中
        $view_status[$i][5]="----";
        $view_status[$i][6]="----";
        $view_status[$i][7]="----";
        $view_status[$i][8]="----";
    }

    $delUrl = "<a href=# onclick='delSolution(".$row['solution_id'].",\"".$row['user_name']."\")'>Delete</a>";
    $view_status[$i][9]= $delUrl;
    $view_status[$i][9].= "&nbsp;/&nbsp;";
    $view_status[$i][9].= "<a target=_self href='../submitpage.php?id=".$row['problem_id']."&sid=".$row['solution_id']."'>Edit</a>";
    $view_status[$i][10]= $row['in_date'];
}

require("../template/".$OJ_TEMPLATE."/admin/status_list_tpl.php");
if( file_exists('../include/cache_end.php') ) {
    require_once('../include/cache_end.php');
}
?>
